package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryInProgressLiveBean;
import com.ymatou.iapi.livebuyer.service.QueryInProgressLiveCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*查询商品进行中的直播
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryInProgressLive{
	private static QueryInProgressLiveBean queryinprogressliveBean;
	private static QueryInProgressLiveCall queryinprogressliveCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商品进行中的直播");
	}

	@Before
	public void caseUp() {
		queryinprogressliveBean=new QueryInProgressLiveBean();
		queryinprogressliveCall=new QueryInProgressLiveCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryinprogressliveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer liveid,Boolean isInSecKill) {
		LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
		JsonObject qdata = queryinprogressliveCall.getJsonObject().get("data").getAsJsonObject();
		Logger.verifyEquals(liveid, qdata.get("liveId").getAsInt(), "验证liveid");
		Logger.verifyEquals(isInSecKill, qdata.get("inSecKill").getAsBoolean(), "验证inSecKill");
		Logger.verifyEquals(LiveRoomCallService.getLivePhase(liveroom.getLiveStatus()), qdata.get("livePhase").getAsInt(), "验证livePhase");
		Logger.verifyEquals(liveroom.getLiveTitle(), qdata.get("liveTitle").getAsString(), "验证liveTitle");
		Logger.verifyEquals(liveroom.getLiveCover(), qdata.get("liveCover").getAsString(), "验证liveCover");
		Map livestatusmap = liveRoomWapper.selectLiveRoomStatById(liveid);
		if(livestatusmap!=null){
			Logger.verifyEquals(livestatusmap.get("ViewNum"), qdata.get("viewNum").getAsInt(), "验证ViewNum");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryInProgressLive_001() {
		Logger.start(true,"查询商品进行中的直播,成功");
		try {
			
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Thread.sleep(1000);
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			verifyDB(createlive.getId(), false);
			Logger.verifyEquals(true,queryinprogressliveCall.getJsonObject().get("data").getAsJsonObject().get("productInLiveRoom").getAsBoolean(), "验证productInLiveRoom-不传sellerid时不返回");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryInProgressLive_002() {
		Logger.start(true,"查询商品进行中的直播-商品秒杀,成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			Date starttime2 = new Date(now.getTime() +  10 * 1000);
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			
			Logger.debug(starttime2);
			Thread.sleep(10000);
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			verifyDB(createlive.getId(), true);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryInProgressLive_003() {
		Logger.start(true,"不输入pid失败");
		try {
			queryinprogressliveBean.setProductId(null);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("100", queryinprogressliveCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryInProgressLive_004() {
		Logger.start(true,"商品不在正在进行的直播中,不返回数据");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			Logger.verifyEquals("null", queryinprogressliveCall.getString("data"), "验证data");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryInProgressLive_005() {
		Logger.start(true,"直播已结束,不返回数据");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			LiveRoomCallService.AutoCloseLiveRoomByUserId(sellerId);
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			Logger.verifyEquals("null", queryinprogressliveCall.getString("data"), "验证data");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryInProgressLive_006() {
		Logger.start(true,"直播未开始,不返回数据");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			Logger.verifyEquals("null", queryinprogressliveCall.getString("data"), "验证data");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryInProgressLive_007() {
		Logger.start(true,"查询商品进行中的直播,成功-传输sellerid");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Thread.sleep(1000);
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveBean.setSellerId(sellerId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			verifyDB(createlive.getId(), false);
			//验证 productInLiveRoom
			Logger.verifyEquals(true,queryinprogressliveCall.getJsonObject().get("data").getAsJsonObject().get("productInLiveRoom").getAsBoolean(), "验证productInLiveRoom");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryInProgressLive_008() {
		Logger.start(true,"查询商品进行中的直播,成功-传输sellerid且商品不在直播中");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
//			Logger.comment("加入商品");
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Thread.sleep(1000);
			queryinprogressliveBean.setProductId(productId);
			queryinprogressliveBean.setSellerId(sellerId);
			queryinprogressliveCall.setData(queryinprogressliveBean);
			queryinprogressliveCall.callService();			
			Logger.verifyEquals("200", queryinprogressliveCall.getString("code"), "验证code");
			verifyDB(createlive.getId(), false);
			Logger.verifyEquals(false,queryinprogressliveCall.getJsonObject().get("data").getAsJsonObject().get("productInLiveRoom").getAsBoolean(), "验证productInLiveRoom");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}